package com.dalor.phonesmanager.adapter;

import android.content.Context;
import android.support.annotation.NonNull;
import android.support.v4.view.PagerAdapter;
import android.text.TextUtils;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

import com.bumptech.glide.Glide;
import com.dalor.phonesmanager.bean.Image;
import com.dalor.phonesmanager.interfaces.OnPhoneItemLongClickListener;
import com.dalor.phonesmanager.view.EnlargeImageView;

import java.io.File;
import java.util.ArrayList;

/**
 * 作者： 吴昶 .
 * 时间: 2019/1/2 9:44
 * 功能简介：图片列表适配器
 */
public class PhonesPagersAdapter extends PagerAdapter {

    private ArrayList<Image> data;
    private Context context;
    private String imagePath;
    private OnPhoneItemLongClickListener longClickListener;

    public PhonesPagersAdapter(Context context,ArrayList<Image> data,OnPhoneItemLongClickListener longClickListener){
        this.context=context;
        this.data=data;
        this.longClickListener=longClickListener;
    }

    public void setData(ArrayList<Image> data) {
        this.data = data;
        notifyDataSetChanged();
    }

    @Override
    public int getCount() {
        return data==null?0:data.size();
    }

    @Override
    public boolean isViewFromObject(@NonNull View view, @NonNull Object object) {
        return view==object;
    }

    @NonNull
    @Override
    public Object instantiateItem(@NonNull ViewGroup container, final int position) {
        ImageView imageView=new EnlargeImageView(context);
        imagePath=data.get(position).path;
        if(!TextUtils.isEmpty(imagePath)) {
            Glide.with(context).load(new File(imagePath)).into(imageView);
        }
        if(longClickListener!=null){
            imageView.setOnLongClickListener(new View.OnLongClickListener() {
                @Override
                public boolean onLongClick(View view) {
                    longClickListener.onItemLongClick(imagePath,position);
                    return false;
                }
            });
        }
        container.addView(imageView,0);
        return imageView;
    }

    @Override
    public void destroyItem(@NonNull ViewGroup container, int position, @NonNull Object object) {
        container.removeView((View)object);
    }

    @Override
    public int getItemPosition(@NonNull Object object) {
        return POSITION_NONE;
    }
}
